﻿/// <reference path="../localStorageHighScore/localStorageOperations.js" />
/// <reference path="../QUnit/qunit-1.11.0.js" />

module("highScore.saveRevord")

test("Is record saved", function () {
    // initialize localStorage to prevent errors.
    localStorage.clear();

    highScore.saveScore("Adrian", 1234);

    var key = localStorage.key(0);
    var actualRecord = localStorage.getObject(key);
    var expectedRecord = { playerName: "Adrian", score: 1234 };

    deepEqual(actualRecord, expectedRecord);
});

test("Multiple records saved", function () {
    // initialize localStorage to prevent errors.
    localStorage.clear();

    highScore.saveScore("Adrian", 1234);
    highScore.saveScore("Sevdalin", 1000);
    highScore.saveScore("Geogri", 900);

    var player1 =  { playerName: "Adrian", score: 1234 };
    var player2 = { playerName: "Sevdalin", score: 1000 };
    var player3 = { playerName: "Geogri", score: 900 };

    var expectedRecords = [player1, player2, player3];

    for (var i = 0; i < 3; i++) {
        var index = localStorage.key(i);
        var actualRecord = localStorage.getObject(index);

        deepEqual(actualRecord, expectedRecords[i], "Testing player" + i);
    }
});

test("Duplicate player name, different score", function () {
    // initialize localStorage to prevent errors.
    localStorage.clear();

    highScore.saveScore("Gosho", 1200);
    highScore.saveScore("Gosho", 1300);

    var player1 = { playerName: "Gosho", score: 1200 };
    var player2 = { playerName: "Gosho", score: 1300 };

    var expectedRecords = [player1, player2];

    for (var i = 0; i < localStorage.length; i++) {
        var index = localStorage.key(i);
        var actualRecord = localStorage.getObject(index);

        deepEqual(actualRecord, expectedRecords[i], "Testing player" + i);
    }
});

test("Duplicate player name, same score", function () {
    // initialize localStorage to prevent errors.
    localStorage.clear();

    highScore.saveScore("Gosho", 1200);
    highScore.saveScore("Gosho", 1200);

    var player1 = { playerName: "Gosho", score: 1200 };
    var player2 = { playerName: "Gosho", score: 1200 };

    var expectedRecords = [player1, player2];

    for (var i = 0; i < localStorage.length; i++) {
        var index = localStorage.key(i);
        var actualRecord = localStorage.getObject(index);

        deepEqual(actualRecord, expectedRecords[i], "Testing player" + i);
    }
});

module("highScore.getTop5Players");

test("Get top 5 from many players", function () {
    // initialize localStorage to prevent errors.
    localStorage.clear();

    highScore.saveScore("Adrian", 5000);
    highScore.saveScore("Pesho", 3200);
    highScore.saveScore("Gosho", 5000);
    highScore.saveScore("Joro", 600);
    highScore.saveScore("Serafim", 7000);
    highScore.saveScore("Nakov", 10000);
    highScore.saveScore("Atanas", 10001);
    highScore.saveScore("Maugli", 600);

    var player0 = { playerName: "Atanas", score: 10001 };
    var player1 = { playerName: "Nakov", score: 10000 };
    var player2 = { playerName: "Serafim", score: 7000 };
    var player3 = { playerName: "Adrian", score: 5000 };
    var player4 = { playerName: "Gosho", score: 5000 };

    var expectedRecords = [player0, player1, player2, player3, player4];
    var actualRecords = highScore.getTop5Players();

    for (var i = 0; i < actualRecords.length; i++) {
        deepEqual(actualRecords[i], expectedRecords[i], "Testing player" + i);
    }
});

test("Get top 5 from many players, duplicate names", function () {
    // initialize localStorage to prevent errors.
    localStorage.clear();

    highScore.saveScore("Adrian", 5000);
    highScore.saveScore("Pesho", 3200);
    highScore.saveScore("Gosho", 5000);
    highScore.saveScore("Joro", 600);
    highScore.saveScore("Serafim", 7000);
    highScore.saveScore("Adrian", 10000);
    highScore.saveScore("Atanas", 10001);
    highScore.saveScore("Maugli", 600);

    var player0 = { playerName: "Atanas", score: 10001 };
    var player1 = { playerName: "Adrian", score: 10000 };
    var player2 = { playerName: "Serafim", score: 7000 };
    var player3 = { playerName: "Adrian", score: 5000 };
    var player4 = { playerName: "Gosho", score: 5000 };

    var expectedRecords = [player0, player1, player2, player3, player4];
    var actualRecords = highScore.getTop5Players();

    for (var i = 0; i < actualRecords.length; i++) {
        deepEqual(actualRecords[i], expectedRecords[i], "Testing player" + i);
    }
});

test("Get top 5 function less than 5 records in localStorage", function () {
    // initialize localStorage to prevent errors.
    localStorage.clear();

    highScore.saveScore("Nakov", 10000);
    highScore.saveScore("Atanas", 10001);
    highScore.saveScore("Maugli", 600);

    var player0 = { playerName: "Atanas", score: 10001 };
    var player1 = { playerName: "Nakov", score: 10000 };
    var player2 = { playerName: "Maugli", score: 600 };

    var expectedRecords = [player0, player1, player2];
    var actualRecords = highScore.getTop5Players();



    for (var i = 0; i < actualRecords.length; i++) {
        console.log(actualRecords[i]);
        console.log(expectedRecords[i]);

        console.log("---------------------");
        deepEqual(actualRecords[i], expectedRecords[i], "Testing player" + i);
    }
});

test("Get top 5 function no records in localStorage", function () {
    // initialize localStorage to prevent errors.
    localStorage.clear();

    var actualRecords = highScore.getTop5Players();

    equal(actualRecords.length, 0, "0 entries");
});

module("highScore.clearHighScore");

test("Clear all records", function () {
    highScore.saveScore("Adrian", 5000);
    highScore.saveScore("Pesho", 3200);
    highScore.saveScore("Gosho", 5000);
    highScore.saveScore("Joro", 600);
    highScore.saveScore("Serafim", 7000);
    highScore.saveScore("Adrian", 10000);
    highScore.saveScore("Atanas", 10001);
    highScore.saveScore("Maugli", 600);

    highScore.clearHighScore();

    equal(localStorage.length, 0, "highScore is clear");
});